package RentCarProject;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Properties;

public class ManagerUI {
    JFrame frame;
    JMenuBar menuBar;
    JMenuItem menu,menu1,menu2,menu3,menu4,menu5,menu6;
    JLabel carlabel;
    MyButton removebutton,updatebutton,addbutton,exitbutton;
    JPanel buttonPanel,orderbuttonPanel;
    JScrollPane scrollPane;
    JSplitPane splitPane,usplitPane,usersplitPane=new JSplitPane();
    JTable table;
    ArrayList<Car> carList=new ArrayList<>();
    Object id=null;
    int row=-1;
    String username=null;
    boolean userflag=false,chargeflag=false;
    public void init() throws Exception {
        frame = new JFrame();
        frame.getRootPane().setBorder(BorderFactory.createLineBorder(Color.cyan, 2));
        frame.setTitle("管理员端");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize( 1200, 1000);
        frame.setLocationRelativeTo(null);

        menuBar=new JMenuBar();
        menu=new JMenuItem("总览车辆信息");
        menu.setPreferredSize(new Dimension(100,20));
        menu1=new JMenuItem("租出车辆信息");
        menu1.setPreferredSize(new Dimension(100,20));
        menu2=new JMenuItem("待租车辆信息");
        menu2.setPreferredSize(new Dimension(100,20));
        menu3=new JMenuItem("车辆热度排行");
        menu3.setPreferredSize(new Dimension(100,20));
        menu4=new JMenuItem("用户信息总览");
        menu4.setPreferredSize(new Dimension(100,20));
        menu5=new JMenuItem("用户充值记录");
        menu5.setPreferredSize(new Dimension(100,20));
        menu6=new JMenuItem("用户租车记录");
        menu6.setPreferredSize(new Dimension(100,20));
        menuBar.add(menu);
        menuBar.add(menu1);
        menuBar.add(menu2);
        menuBar.add(menu3);
        menuBar.add(menu4);
        menuBar.add(menu5);
        menuBar.add(menu6);
        menuBar.setBounds(0,0,1000,20);

        menu.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(userflag){
                    frame.remove(usersplitPane);
                    frame.add(splitPane);
                    userflag=false;
                }else if(chargeflag){
                    frame.remove(scrollPane);
                    frame.add(splitPane);
                    chargeflag=false;
                }
                splitPane.setDividerLocation(520);
                splitPane.setLeftComponent(scrollPane);
                try {
                    refreshTabel();
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
                frame.revalidate();
                frame.repaint();
            }
        });
        menu1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(userflag){
                    frame.remove(usersplitPane);
                    frame.add(splitPane);
                    userflag=false;
                }else if(chargeflag){
                    frame.remove(scrollPane);
                    frame.add(splitPane);
                    chargeflag=false;
                }
                splitPane.setDividerLocation(520);
                splitPane.setEnabled(false);
                splitPane.setLeftComponent(scrollPane);
                try {
                    rentTable();
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
                frame.revalidate();
                frame.repaint();
            }
        });
        menu2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(userflag){
                    frame.remove(usersplitPane);
                    frame.add(splitPane);
                    userflag=false;
                }else if(chargeflag){
                    frame.remove(scrollPane);
                    frame.add(splitPane);
                    chargeflag=false;
                }
                splitPane.setDividerLocation(520);
                splitPane.setEnabled(false);
                splitPane.setLeftComponent(scrollPane);
                try {
                    notrentTable();
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
                frame.revalidate();
                frame.repaint();
            }
        });
        menu3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(userflag){
                    frame.remove(usersplitPane);
                    frame.add(splitPane);
                    userflag=false;
                }else if(chargeflag){
                    frame.remove(scrollPane);
                    frame.add(splitPane);
                    chargeflag=false;
                }
                splitPane.setDividerLocation(520);
                splitPane.setEnabled(false);
                splitPane.setLeftComponent(scrollPane);
                try {
                    sortTable();
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
                frame.revalidate();
                frame.repaint();
            }
        });
        menu4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(chargeflag){
                    frame.remove(scrollPane);
                    frame.add(usersplitPane);
                    chargeflag=false;
                    userflag=true;
                }else if(!userflag){
                    frame.remove(splitPane);
                    frame.add(usersplitPane);
                    userflag=true;
                }
                try {
                    UserPerformanceFrame();
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
                System.out.println("123");
                frame.revalidate();
                frame.repaint();
            }
        });
        menu5.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(userflag){
                    frame.remove(usersplitPane);
                    frame.add(scrollPane);
                    userflag=false;
                    chargeflag=true;
                }else if(!chargeflag){
                    frame.remove(splitPane);
                    frame.add(scrollPane);
                    chargeflag=true;
                }
                try {
                    chargeTable();
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
                frame.revalidate();
                frame.repaint();
            }
        });
        menu6.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(userflag){
                    frame.remove(usersplitPane);
                    frame.add(splitPane);
                    userflag=false;
                }else if(chargeflag){
                    frame.remove(scrollPane);
                    frame.add(splitPane);
                    chargeflag=false;
                }
                splitPane.setDividerLocation(520);
                splitPane.setEnabled(false);
                splitPane.setLeftComponent(scrollPane);
                try {
                    reportTable();
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
                frame.revalidate();
                frame.repaint();
            }
        });

        table=new JTable();
        table.setFont(new Font("宋体",Font.BOLD,14));
        table.setRowHeight(25);
        DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
        centerRenderer.setHorizontalAlignment(SwingConstants.CENTER);
        table.setDefaultRenderer(Object.class, centerRenderer);
        table.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if(e.getClickCount()==1){
                    row=table.getSelectedRow();
                    id=table.getModel().getValueAt(row,0);
                    try {
                        carlabel.setIcon(new ImageIcon(searchPictureAddress(id)));
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                }
            }
        });

        refreshTabel();

        carlabel=new JLabel();
        carlabel.setIcon(new ImageIcon("src/picture/start.png"));

        removebutton=new MyButton("移除车辆");
        removebutton.setBounds(10,8,150,90);
        removebutton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(row!=-1){
                    int a = JOptionPane.showConfirmDialog(frame, "确认移除编号为" + table.getModel().getValueAt(row, 0) + "的" + table.getModel().getValueAt(row, 1) + "?", "移除车辆", JOptionPane.YES_NO_OPTION);
                    if (a == JOptionPane.YES_OPTION) {
                        try {
                            removeCarbyid(id);
                            refreshTabel();
                            JOptionPane.showMessageDialog(frame, "移除成功！");
                        } catch (Exception ex) {
                            throw new RuntimeException(ex);
                        }
                    } else {
                        JOptionPane.showMessageDialog(frame, "移除车辆失败！");
                    }
                }else{
                    JOptionPane.showMessageDialog(frame,"请选择要移除的车辆！");
                }
            }
        });

        updatebutton=new MyButton("修改车辆信息");
        updatebutton.setBounds(10,106,150,90);
        updatebutton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                UpdateCarPerformance updateCarPerformance =new UpdateCarPerformance();
                updateCarPerformance.carid=id;
                try {
                    updateCarPerformance.init();
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            }
        });

        addbutton=new MyButton("增加车辆");
        addbutton.setBounds(10,204,150,90);
        addbutton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    new AddCarPerformance().init();
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            }
        });

        exitbutton=new MyButton("退出");
        exitbutton.setBounds(10,302,150,90);
        exitbutton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new LoginUI();
                frame.dispose();
            }
        });

        buttonPanel = new JPanel();
        buttonPanel.setLayout(null);
        buttonPanel.add(removebutton);
        buttonPanel.add(updatebutton);
        buttonPanel.add(addbutton);
        buttonPanel.add(exitbutton);

        scrollPane = new JScrollPane(table);
        usplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
        usplitPane.setDividerLocation(1000);
        usplitPane.setEnabled(false);
        usplitPane.setLeftComponent(carlabel);
        usplitPane.setRightComponent(buttonPanel);
        splitPane=new JSplitPane();
        splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
        splitPane.setDividerLocation(520);
        splitPane.setEnabled(false);
        splitPane.setLeftComponent(scrollPane);
        splitPane.setRightComponent(usplitPane);

        frame.add(menuBar,BorderLayout.NORTH);
        frame.add(splitPane);
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setVisible(true);
    }

    public void refreshTabel() throws Exception {
        carList.clear();
        Properties properties=new Properties();
        properties.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource= DruidDataSourceFactory.createDataSource(properties);
        Connection connection=dataSource.getConnection();
        String sql="select * from car";
        ResultSet resultSet=connection.createStatement().executeQuery(sql);
        while(resultSet.next()){
            Car car=new Car();
            car.setId(resultSet.getInt("id"));
            car.setName(resultSet.getString("name"));
            car.setColor(resultSet.getString("color"));
            car.setType(resultSet.getString("type"));
            car.setState(resultSet.getBoolean("status"));
            car.setRent(resultSet.getInt("rent"));
            car.setRenttime(resultSet.getString("renttime"));
            car.setReturntime(resultSet.getString("returntime"));
            car.setAddtime(resultSet.getString("addtime"));
            car.setRenter(resultSet.getString("renter"));
            carList.add(car);
        }
        resultSet.close();
        connection.close();
        DefaultTableModel model=new DefaultTableModel(new Object[]{"编号","名称","颜色","车型","状态","租金","最后租出时间","最后还车时间","入库时间","当前租车用户"},0);
        for(Car car:carList){
            model.addRow(new Object[]{car.getId(),car.getName(),car.getColor(),car.getType(),statusToString(car.getState()),car.getRent(),car.getRenttime(),car.getReturntime(),car.getAddtime(),car.getRenter()});
        }
        table.setModel(model);
    }

    public void rentTable() throws Exception {
        DefaultTableModel model=new DefaultTableModel(new Object[]{"编号","名称","颜色","车型","状态","租金（元/天）","租出时间","租赁天数","租车用户","支付租金"},0);
        Properties properties=new Properties();
        properties.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource= DruidDataSourceFactory.createDataSource(properties);
        Connection connection=dataSource.getConnection();
        String sql="select * from car where status=0";
        ResultSet resultSet=connection.createStatement().executeQuery(sql);
        while(resultSet.next()){
            model.addRow(new Object[]{resultSet.getInt("id"),resultSet.getString("name"),resultSet.getString("color"),resultSet.getString("type"),statusToString(resultSet.getBoolean("status")),resultSet.getInt("rent"),resultSet.getString("renttime"),resultSet.getString("days"),resultSet.getString("renter"),resultSet.getInt("rent")*resultSet.getInt("days")});
        }
        table.setModel(model);
    }

    public void notrentTable() throws Exception {
        DefaultTableModel model=new DefaultTableModel(new Object[]{"编号","名称","颜色","车型","状态","租金","入库时间"},0);
        Properties properties=new Properties();
        properties.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource= DruidDataSourceFactory.createDataSource(properties);
        Connection connection=dataSource.getConnection();
        String sql="select * from car where status=1";
        ResultSet resultSet=connection.createStatement().executeQuery(sql);
        while(resultSet.next()){
            model.addRow(new Object[]{resultSet.getInt("id"),resultSet.getString("name"),resultSet.getString("color"),resultSet.getString("type"),statusToString(resultSet.getBoolean("status")),resultSet.getInt("rent"),resultSet.getString("addtime")});
        }
        table.setModel(model);
    }

    public void sortTable() throws Exception {
        DefaultTableModel model=new DefaultTableModel(new Object[]{"编号","名称","颜色","车型","状态","租金","入库时间","租赁次数"},0);
        Properties properties=new Properties();
        properties.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource= DruidDataSourceFactory.createDataSource(properties);
        Connection connection=dataSource.getConnection();
        String sql="select * from car order by count desc";
        ResultSet resultSet=connection.createStatement().executeQuery(sql);
        while(resultSet.next()){
            model.addRow(new Object[]{resultSet.getInt("id"),resultSet.getString("name"),resultSet.getString("color"),resultSet.getString("type"),statusToString(resultSet.getBoolean("status")),resultSet.getInt("rent"),resultSet.getString("addtime"),resultSet.getInt("count")});
        }
        table.setModel(model);
    }

    public void userTable(JTable table) throws Exception {
        DefaultTableModel model=new DefaultTableModel(new Object[]{"用户名","账户密码","账户余额"},0);
        Properties properties=new Properties();
        properties.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource= DruidDataSourceFactory.createDataSource(properties);
        Connection connection=dataSource.getConnection();
        String sql="select * from user";
        ResultSet resultSet=connection.createStatement().executeQuery(sql);
        while(resultSet.next()){
            model.addRow(new Object[]{resultSet.getString("username"), resultSet.getString("password"), resultSet.getInt("account")});
        }
        table.setModel(model);
    }

    public void chargeTable() throws Exception {
        DefaultTableModel model=new DefaultTableModel(new Object[]{"充值账户","充值时间","充值金额"},0);
        Properties properties=new Properties();
        properties.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource= DruidDataSourceFactory.createDataSource(properties);
        Connection connection=dataSource.getConnection();
        String sql="select * from charge";
        ResultSet resultSet=connection.createStatement().executeQuery(sql);
        while(resultSet.next()){
            model.addRow(new Object[]{resultSet.getString("user"), resultSet.getString("chargetime"), resultSet.getInt("account")});
        }
        table.setModel(model);
    }

    public void reportTable() throws Exception {
        DefaultTableModel model=new DefaultTableModel(new Object[]{"编号","名称","颜色","车型","租赁时间","还车时间","租赁天数","支付金额","租赁人"},0);
        Properties properties=new Properties();
        properties.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource= DruidDataSourceFactory.createDataSource(properties);
        Connection connection=dataSource.getConnection();
        String sql="select * from rentreport";
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        ResultSet resultSet=preparedStatement.executeQuery();
        while(resultSet.next()){
            model.addRow(new Object[]{resultSet.getInt("carid"),resultSet.getString("carname"),resultSet.getString("carcolor"),resultSet.getString("cartype"),resultSet.getString("renttime"),resultSet.getString("returntime"),resultSet.getInt("days"),resultSet.getInt("account"),resultSet.getString("user")});
        }
        table.setModel(model);
    }

    public String statusToString(boolean status){
        if(status){
            return "待租";
        }else {
            return "已租出";
        }
    }

    public void removeCarbyid(Object id) throws Exception {
        Properties properties=new Properties();
        properties.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource= DruidDataSourceFactory.createDataSource(properties);
        Connection connection=dataSource.getConnection();
        String sql="delete from car where id=?";
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        preparedStatement.setString(1,id.toString());
        preparedStatement.executeUpdate();
        preparedStatement.close();
        connection.close();
    }

    public String searchPictureAddress(Object id) throws Exception {
        Properties properties=new Properties();
        properties.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource= DruidDataSourceFactory.createDataSource(properties);
        Connection connection=dataSource.getConnection();
        String sql="select * from car where id=?";
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        preparedStatement.setString(1,String.valueOf(id));
        ResultSet resultSet=preparedStatement.executeQuery();
        if(resultSet.next()&&resultSet.getString("picture")!=null){
            String a=resultSet.getString("picture");
            resultSet.close();
            preparedStatement.close();
            connection.close();
            return a;
        }else{
            resultSet.close();
            preparedStatement.close();
            connection.close();
            return "error.png";
        }
    }

    public void userTableByQuery(JTable table,String username) throws Exception {
        DefaultTableModel model=new DefaultTableModel(new Object[]{"用户名","账户密码","账户余额"},0);
        Properties properties=new Properties();
        properties.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource= DruidDataSourceFactory.createDataSource(properties);
        Connection connection=dataSource.getConnection();
        String sql="select * from user where username='"+username+"'";
        ResultSet resultSet=connection.createStatement().executeQuery(sql);
        while(resultSet.next()){
            model.addRow(new Object[]{resultSet.getString("username"),resultSet.getString("password"),resultSet.getInt("account")});
        }
        table.setModel(model);
    }

    public UserBase getUserByUsername(String username) throws Exception {
        Properties properties=new Properties();
        properties.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource= DruidDataSourceFactory.createDataSource(properties);
        Connection connection=dataSource.getConnection();
        String sql="select * from user where username='"+username+"'";
        ResultSet resultSet=connection.createStatement().executeQuery(sql);
        if(resultSet.next()){
            UserBase user=new UserBase();
            user.setUsername(resultSet.getString("username"));
            user.setPassword(resultSet.getString("password"));
            user.setAccount(resultSet.getInt("account"));
            resultSet.close();
            connection.close();
            return user;
        }else {
            resultSet.close();
            connection.close();
            return null;
        }
    }

    public boolean searchUserPerformance(String username) throws Exception {
        Properties properties=new Properties();
        properties.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource= DruidDataSourceFactory.createDataSource(properties);
        Connection connection=dataSource.getConnection();
        String sql="select * from user where username=?";
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        preparedStatement.setString(1,username);
        ResultSet resultSet=preparedStatement.executeQuery();
        if(resultSet.next()){
            resultSet.close();
            preparedStatement.close();
            connection.close();
            return true;
        }
        resultSet.close();
        preparedStatement.close();
        connection.close();
        return false;
    }

    public void UserPerformanceFrame() throws Exception {
        JTable usertable=new JTable();
        userTable(usertable);
        JScrollPane userscrollpane=new JScrollPane(usertable);
        usersplitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
        usersplitPane.setDividerLocation(800);
        usersplitPane.setEnabled(false);
        usersplitPane.setLeftComponent(userscrollpane);

        usertable.setFont(new Font("宋体",Font.BOLD,14));
        usertable.setRowHeight(25);
        DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
        centerRenderer.setHorizontalAlignment(SwingConstants.CENTER);
        usertable.setDefaultRenderer(Object.class, centerRenderer);
        usertable.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if(e.getClickCount()==1){
                    int row=usertable.getSelectedRow();
                    username=usertable.getModel().getValueAt(row,0).toString();
                }
            }
        });

        MyButton searchbutton=new MyButton("查找用户信息");
        searchbutton.setBounds(45,10,200,100);
        searchbutton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String answer=JOptionPane.showInputDialog(frame,"请输入要查找的账号：");
                if(answer!=null&&!answer.isEmpty()) {
                    try {
                        if (searchUserPerformance(answer)) {
                            userTableByQuery(usertable, answer);
                        } else {
                            JOptionPane.showMessageDialog(frame, "未查询到该账号！");
                        }
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                }else if(answer!=null&&answer.isEmpty()){
                    JOptionPane.showMessageDialog(frame,"查询信息不能为空！");
                }
            }
        });
        MyButton updatebutton=new MyButton("修改用户信息");
        updatebutton.setBounds(265,10,200,100);
        updatebutton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                UpdateUserPerformanceFrame updateuserperformanceframe=new UpdateUserPerformanceFrame(usertable);
                updateuserperformanceframe.init();
            }
        });
        MyButton addbutton=new MyButton("增加用户信息");
        addbutton.setBounds(485,10,200,100);
        addbutton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                AddUserPerformanceFrame adduserperformanceframe=new AddUserPerformanceFrame(usertable);
                adduserperformanceframe.init();
            }
        });
        MyButton deletebutton=new MyButton("删除用户信息");
        deletebutton.setBounds(705,10,200,100);
        deletebutton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String deleteusername=JOptionPane.showInputDialog(frame,"请输入要删除账号的用户名：");
                if(deleteusername!=null&&!deleteusername.isEmpty()){
                    try {
                        if(searchUserPerformance(deleteusername)){
                            if(DeleteUserPerformance(deleteusername)){
                                JOptionPane.showMessageDialog(frame,"删除成功！");
                            }else{
                                JOptionPane.showMessageDialog(frame,"删除失败！");
                            }
                        }else{
                            JOptionPane.showMessageDialog(frame,"该账户不存在！");
                        }
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                }else if(deleteusername!=null&&deleteusername.isEmpty()){
                    JOptionPane.showMessageDialog(frame,"输入信息为空！");
                }
            }
        });
        MyButton exitbutton=new MyButton("退出管理员系统");
        exitbutton.setBounds(925,10,200,100);
        exitbutton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.dispose();
                new LoginUI();
            }
        });

        JPanel userbuttonPanel=new JPanel();
        userbuttonPanel.setLayout(null);
        userbuttonPanel.add(searchbutton);
        userbuttonPanel.add(updatebutton);
        userbuttonPanel.add(addbutton);
        userbuttonPanel.add(deletebutton);
        userbuttonPanel.add(exitbutton);
        usersplitPane.setRightComponent(userbuttonPanel);

        frame.add(usersplitPane);
        frame.revalidate();
        frame.repaint();
    }

    public boolean UpdateUserPerformance(UserBase userBase,String oldname) throws Exception {
        Properties properties=new Properties();
        properties.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource= DruidDataSourceFactory.createDataSource(properties);
        Connection connection=dataSource.getConnection();
        String sql="update user set username=?,password=?,account=? where username=?";
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        preparedStatement.setString(1,userBase.getUsername());
        preparedStatement.setString(2,userBase.getPassword());
        preparedStatement.setInt(3,userBase.getAccount());
        preparedStatement.setString(4,oldname);
        int a=preparedStatement.executeUpdate();
        if(a>0){
            preparedStatement.close();
            connection.close();
            return true;
        }else{
            preparedStatement.close();
            connection.close();
            return false;
        }
    }

    public class UpdateUserPerformanceFrame{
        JFrame updateuserframe=new JFrame();
        JLabel oldusernamelabel,usernamelabel,passwordlabel,accountlabel,updateuserperformancebglabel;
        JTextField oldusernameField,usernameField,passwordField,accountField;
        MyButton updateuserbutton,exitbutton;
        UserBase userBase=new UserBase();
        JTable table;
        public UpdateUserPerformanceFrame(JTable table){
            this.table=table;
        }
        public void init(){
            updateuserframe.setTitle("修改用户账号信息");
            updateuserframe.setLayout(null);
            updateuserframe.setSize(500,500);
            updateuserframe.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
            updateuserframe.setLocationRelativeTo(null);

            updateuserperformancebglabel=new JLabel(new ImageIcon("src/picture/userperformanceoperatebg.png"));
            updateuserperformancebglabel.setOpaque(false);
            updateuserperformancebglabel.setBounds(0,0,500,500);
            oldusernamelabel=new JLabel("原用户名：");
            oldusernamelabel.setFont(new Font("宋体", Font.BOLD,15));
            oldusernamelabel.setBounds(100,60,100,40);
            usernamelabel=new JLabel("新用户名：");
            usernamelabel.setFont(new Font("宋体", Font.BOLD,15));
            usernamelabel.setBounds(100,120,100,40);
            passwordlabel=new JLabel("新密码：");
            passwordlabel.setFont(new Font("宋体", Font.BOLD,15));
            passwordlabel.setBounds(100,180,100,40);
            accountlabel=new JLabel("账户余额：");
            accountlabel.setFont(new Font("宋体", Font.BOLD,15));
            accountlabel.setBounds(100,240,100,40);
            oldusernameField=new JTextField();
            oldusernameField.setBounds(200,60,200,40);
            usernameField=new JTextField();
            usernameField.setBounds(200,120,200,40);
            passwordField=new JPasswordField();
            passwordField.setBounds(200,180,200,40);
            accountField=new JTextField();
            accountField.setBounds(200,240,200,40);

            try {
                if(username!=null){
                    userBase = getUserByUsername(username);
                }
                System.out.println(userBase);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }

            if(userBase!=null){
                oldusernameField.setText(userBase.getUsername());
                passwordField.setText(userBase.getPassword());
                accountField.setText(String.valueOf(userBase.getAccount()));
            }

            updateuserbutton =new MyButton("提交更改");
            updateuserbutton.setBounds(125,350,100,50);
            updateuserbutton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if(usernameField.getText().isEmpty() || passwordField.getText().isEmpty() || accountField.getText().isEmpty()){
                        JOptionPane.showMessageDialog(updateuserframe,"请填写完整信息！");
                    }else {
                        try {
                            userBase.setUsername(usernameField.getText());
                            userBase.setPassword(passwordField.getText());
                            userBase.setAccount(Integer.parseInt(accountField.getText()));
                            String oldname=oldusernameField.getText();
                            if(UpdateUserPerformance(userBase,oldname)){
                                userTable(table);
                                JOptionPane.showMessageDialog(updateuserframe,"修改信息成功！");
                            }else{
                                JOptionPane.showMessageDialog(updateuserframe,"修改信息失败！");
                            }
                        } catch (Exception ex) {
                            throw new RuntimeException(ex);
                        }
                    }
                }
            });
            exitbutton=new MyButton("退出");
            exitbutton.setBounds(275,350,100,50);
            exitbutton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    updateuserframe.dispose();
                }
            });

            updateuserframe.add(oldusernamelabel);
            updateuserframe.add(oldusernameField);
            updateuserframe.add(usernamelabel);
            updateuserframe.add(usernameField);
            updateuserframe.add(passwordlabel);
            updateuserframe.add(passwordField);
            updateuserframe.add(accountlabel);
            updateuserframe.add(accountField);
            updateuserframe.add(updateuserbutton);
            updateuserframe.add(exitbutton);
            updateuserframe.add(updateuserperformancebglabel);
            updateuserframe.setVisible(true);
        }
    }

    public boolean AddUserPerformance(UserBase userBase) throws Exception {
        Properties properties=new Properties();
        properties.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource= DruidDataSourceFactory.createDataSource(properties);
        Connection connection=dataSource.getConnection();
        String sql="insert into user values(?,?,?)";
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        preparedStatement.setString(1,userBase.getUsername());
        preparedStatement.setString(2,userBase.getPassword());
        preparedStatement.setInt(3,userBase.getAccount());
        int a=preparedStatement.executeUpdate();
        if(a>0){
            preparedStatement.close();
            connection.close();
            return true;
        }else {
            preparedStatement.close();
            connection.close();
            return false;
        }
    }

    public class AddUserPerformanceFrame extends JFrame{
        JFrame adduserframe=new JFrame();
        JLabel usernamelabel,passwordlabel,accountlabel,adduserperformancelabel;
        JTextField usernameField,passwordField,accountField;
        MyButton adduserbutton,exitbutton;
        JTable table;
        public AddUserPerformanceFrame(JTable table) {
            this.table=table;
        }
        public void init(){
            adduserframe.setTitle("添加用户信息");
            adduserframe.setLayout(null);
            adduserframe.setSize(500,500);
            adduserframe.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
            adduserframe.setLocationRelativeTo(null);

            adduserperformancelabel=new JLabel(new ImageIcon("src/picture/userperformanceoperatebg.png"));
            adduserperformancelabel.setOpaque(false);
            adduserperformancelabel.setBounds(0,0,500,500);
            usernamelabel=new JLabel("用户名：");
            usernamelabel.setFont(new Font("宋体", Font.BOLD,15));
            usernamelabel.setBounds(100,80,100,40);
            passwordlabel=new JLabel("密码：");
            passwordlabel.setFont(new Font("宋体", Font.BOLD,15));
            passwordlabel.setBounds(100,150,100,40);
            accountlabel=new JLabel("账户余额：");
            accountlabel.setFont(new Font("宋体", Font.BOLD,15));
            accountlabel.setBounds(100,220,100,40);
            usernameField=new JTextField();
            usernameField.setBounds(200,80,200,40);
            passwordField=new JPasswordField();
            passwordField.setBounds(200,150,200,40);
            accountField=new JTextField();
            accountField.setBounds(200,220,200,40);

            adduserbutton=new MyButton("添加");
            adduserbutton.setBounds(125,330,100,50);
            adduserbutton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if(usernameField.getText().isEmpty() || passwordField.getText().isEmpty() || accountField.getText().isEmpty()){
                        JOptionPane.showMessageDialog(adduserframe,"请输入完整信息！");
                    }else{
                        UserBase userBase=new UserBase();
                        userBase.setUsername(usernameField.getText());
                        userBase.setPassword(passwordField.getText());
                        userBase.setAccount(Integer.parseInt(accountField.getText()));
                        try {
                            if(searchUserPerformance(userBase.getUsername())){
                                JOptionPane.showMessageDialog(adduserframe,"用户名已存在！");
                            }else{
                                if(AddUserPerformance(userBase)){
                                    JOptionPane.showMessageDialog(adduserframe,"添加成功！");
                                    userTable(table);
                                }else{
                                    JOptionPane.showMessageDialog(adduserframe,"添加失败！");
                                }
                            }
                        } catch (Exception ex) {
                            throw new RuntimeException(ex);
                        }
                    }
                }
            });
            exitbutton=new MyButton("退出");
            exitbutton.setBounds(275,330,100,50);
            exitbutton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    adduserframe.dispose();
                }
            });

            adduserframe.add(usernamelabel);
            adduserframe.add(usernameField);
            adduserframe.add(passwordlabel);
            adduserframe.add(passwordField);
            adduserframe.add(accountlabel);
            adduserframe.add(accountField);
            adduserframe.add(adduserbutton);
            adduserframe.add(exitbutton);
            adduserframe.add(adduserperformancelabel);
            adduserframe.setVisible(true);
        }
    }

    public boolean DeleteUserPerformance(String username) throws Exception {
        Properties properties=new Properties();
        properties.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource= DruidDataSourceFactory.createDataSource(properties);
        Connection connection=dataSource.getConnection();
        String sql="delete from user where username='"+username+"'";
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        int a=preparedStatement.executeUpdate();
        if(a>0){
            preparedStatement.close();
            connection.close();
            return true;
        }else{
            preparedStatement.close();
            connection.close();
            return false;
        }
    }

    public static void main(String[] args) throws Exception {
        new ManagerUI().init();
    }

}
